package vpfarm.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;

import vpfarm.Constants;

public class JobInfo implements Serializable {
	
	
	private int jobID = 0;
	private String jobName = null;
	
	private String blenderFile = null;
	private byte[] blenderFileByteData = null;
	private boolean blenderFileStored = false;
	
	private int start = 0;
	private int end = 100;
	private int step = 1;
	private int format = 0;
	private long renderTimeout = 60*60*1000;
	
	public JobInfo(int id, String name, String f, int start, int end, int step, int format, long rto) throws IOException{
		this.jobID = id;
		this.jobName = name;
		this.start = start;
		this.end = end;
		this.step = step;
		this.format = format;
		this.renderTimeout = rto;
		
		File file = new File(f);
		this.blenderFileByteData = new byte[(int) file.length()];
		FileInputStream f_in = new FileInputStream(f);
		f_in.read(this.blenderFileByteData);
		f_in.close();
		
		this.blenderFileStored = false;
	}
	
	
	public JobInfo(JobInfo j) throws IOException {
		this(j.jobID, j.jobName, j.blenderFile, j.start, j.end, j.step, j.format, j.renderTimeout);
	}


	public JobInfo(int i, String name, String blenderFile2, int start2, int end2, int step2, String formatString, long rt) throws IOException {
		this(i, name, blenderFile2, start2, end2, step2, stringToFormat(formatString), rt);
	}
	

	private JobInfo() {
		// TODO Auto-generated constructor stub
	}


	public static int stringToFormat(String s){
		if (s.equals("PNG"))
			return Constants.FORMAT_PNG;
		else if (s.equals("JPG"))
			return Constants.FORMAT_JPG;
		return Constants.FORMAT_JPG;
	}

	public static String formatToString(int format){
		if (format == Constants.FORMAT_JPG)
			return "JPG";
		else if (format == Constants.FORMAT_PNG)
			return "PNG";
		else
			return "JPG";
	}
	
	public String formatToString(){
		return formatToString(format);
	}

	public String getBlenderFile() {
		return blenderFile;
	}

	public void setBlenderFile(String blenderFile) {
		this.blenderFile = blenderFile;
	}

	public int getEnd() {
		return end;
	}

	public void setEnd(int end) {
		this.end = end;
	}

	public int getFormat() {
		return format;
	}

	public void setFormat(int format) {
		this.format = format;
	}

	public int getJobID() {
		return jobID;
	}

	public void setJobID(int jobID) {
		this.jobID = jobID;
	}

	public String getJobName() {
		return jobName;
	}

	public void setJobName(String jobName) {
		this.jobName = jobName;
	}

	public long getRenderTimeout() {
		return renderTimeout;
	}

	public void setRenderTimeout(long renderTimeout) {
		this.renderTimeout = renderTimeout;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getStep() {
		return step;
	}

	public void setStep(int step) {
		this.step = step;
	}

	public byte[] getBlenderFileByteData() {
		return blenderFileByteData;
	}

	public void setBlenderFileByteData(byte[] blenderFileByteData) {
		this.blenderFileByteData = blenderFileByteData;
	}

	public boolean isBlenderFileStored() {
		return blenderFileStored;
	}

	public void setBlenderFileStored(boolean blenderFileStored) {
		this.blenderFileStored = blenderFileStored;
	}


	public void saveFileTo(String blenderFilePath) throws IOException {
		if (!this.blenderFileStored){
			FileOutputStream f_out = new FileOutputStream(blenderFilePath);
			f_out.write(this.blenderFileByteData);
			f_out.close();
			this.blenderFile = blenderFilePath;
			this.blenderFileStored = true;
		}
	}


	public JobInfo copy() {
		JobInfo out = new JobInfo();
		out.blenderFile = this.blenderFile;
		out.blenderFileStored = true;
		out.end = end;
		out.format = format;
		out.jobID = jobID;
		out.jobName = jobName;
		out.renderTimeout = renderTimeout;
		out.start = start;
		out.step = step;
		return out;
	}
	
	
}
